﻿// SLF.NET - Simple Logging Façade for .NET
// Contact and Information: http://slf.codeplex.com
//
// This library is free software; you can redistribute it and/or
// modify it in any way you see fit as long as this copyright
// notice is not being removed.
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE



using System;
using System.Windows.Browser;


namespace Slf.Silverlight
{
  public class SLDebugLogger : FormattableLoggerBase
  {
    private const string JsDebugExec = "slfDebug('{0}');";

    private const string JsDebug = @"function slfDebug(msg)
                {        // VS script debugger output window.
                        if ((typeof(Debug) !== 'undefined') && Debug.writeln) {
                            Debug.writeln(msg);
                        }
                        // FF firebug and Safari console.
                        if (window.console && window.console.log) {
                            window.console.log(msg);
                        }
                        // Opera console.
                        if (window.opera) {
                            window.opera.postError(msg);
                        }
                        // WebDevHelper console.
                        if (window.debugService) {
                            window.debugService.trace(msg);
                        }
                };";


    public SLDebugLogger(string name) : base(name)
    {
    }

    public SLDebugLogger()
    {
    }


    /// <summary>
    /// Logs a given item to both the broser console and the debugger.
    /// </summary>
    /// <param name="item">The item to be logged.</param>
    /// <exception cref="ArgumentNullException">If <paramref name="item"/>
    /// is a null reference.</exception>
    public override void Log(LogItem item)
    {
      Ensure.ArgumentNotNull(item, "item");

      //format message...
      string message = FormatItem(item) ?? "";
      
      //write formatted message to debug
      System.Diagnostics.Debug.WriteLine(message);
      
      //escape backspace characters
      message = message.Replace(@"\", @"\\");
      //...turn line breaks into line break strings (interpreted in JavaScript)
      message = message.Replace("\r\n", @"\n").Replace("\n", @"\n").Replace("\r", @"\n");
      //escape tabs
      message = message.Replace("\t", @"\t");
      //...and escape apostrophes and quotes
      message = message.Replace("'", "\\'").Replace("\"", "\\\"");
      
      //create and run script
      string script = JsDebug + "\r\n" + String.Format(JsDebugExec, message);
      HtmlPage.Window.Eval(script);
    }
  }
}
